 
;;=============================================Question part========================================================
(defrule education_input    
    (declare (salience -7))    
    (logical 
         (begin_background))
=>
        (assert (UI-state (display education_input)
                     (relation-asserted source_edu_choice)
                     (response edu1)    
                     (valid-answers edu1 edu2 edu3 edu4 edu5))))  

(defrule convert_edu_choice
    (logical (source_edu_choice ?c))
=>    (switch ?c 
        (case edu1 then (assert (source_edu 1)))
        (case edu2 then (assert (source_edu 2)))
        (case edu3 then (assert (source_edu 3)))
        (case edu4 then (assert (source_edu 4)))
        (case edu5 then (assert (source_edu 5))))
)

(defrule career_input  
    (logical (source_edu ?s))
=>
        (assert (UI-state (display career_input)
                     (relation-asserted source_car)
                     (answer-type dropdown)     ;comment until dropdown type is supported
                     (response career1)    
                     (valid-answers career1 career2 career3 career4 career5 career6 career7 career8 career9 career10 career11 
                        career12 career13 career14 career15 career16))))

(defrule income_input
    (logical (source_car ?c))
=>
        (assert (UI-state (display income_input)
                     (relation-asserted fin_choice)
                     (response income1)    
                     (valid-answers income1 income2 income3 income4 income5))))

;(1-below 20k/2-20k~40k/3-40k~80k/4-80k~150k/5-above 150k)
(defrule convert_income_choice
    (logical (fin_choice ?c))
=>    (switch ?c 
        (case income1 then (assert (fin_result 1)))
        (case income2 then (assert (fin_result 2)))
        (case income3 then (assert (fin_result 3)))
        (case income4 then (assert (fin_result 4)))
        (case income5 then (assert (fin_result 5))))
)


(defrule asset_input
    (logical (fin_result ?c))
=>
        (assert (UI-state (display asset_input)
                     (relation-asserted source_asset_yn)
                     (response y)    
                     (valid-answers y n))))

(defrule asset_yes
    (logical (source_asset_yn y))
=>
        (assert (UI-state (display asset_yes)
                     (relation-asserted source_asset)
                     (response asset1)    
                     (valid-answers asset1 asset2 asset3)))
        (assert (asset_result 0)))    ;;for later use 

(defrule asset_choice1
    (logical (or (source_asset asset1) (source_asset asset3)))
=>
        (assert (UI-state (display asset_choice1)
                     (relation-asserted source_asset_car)
                     (response asset_car1)    
                     (valid-answers asset_car1 asset_car2 asset_car3 asset_car4 asset_car5))))

(defrule asset_choice2
    (logical (or (source_asset asset2) (source_asset asset3)))
=>
        (assert (UI-state (display asset_choice2)
                     (relation-asserted source_asset_estate)
                     (response asset_estate1)    
                     (valid-answers asset_estate1 asset_estate2 asset_estate3))))

(defrule convert_asset_choice_car
    (logical (source_asset_car ?c))
=>    (switch ?c 
        (case asset_car1 then (assert (asset_car_score 1)))
        (case asset_car2 then (assert (asset_car_score 2)))
        (case asset_car3 then (assert (asset_car_score 3)))
        (case asset_car4 then (assert (asset_car_score 4)))
        (case asset_car5 then (assert (asset_car_score 5))))
)
(defrule convert_asset_choice_estate
    (logical (source_asset_estate ?c))
=>    (switch ?c 
        (case asset_estate1 then (assert (asset_estate_score 1)))
        (case asset_estate2 then (assert (asset_estate_score 2)))
        (case asset_estate3 then (assert (asset_estate_score 3))))
)

(defrule combine_asset_choice_car
    ?f1 <- (asset_score_car ?c)
    ?f2 <- (asset_result ?r)
=>
    (retract ?f2)
    (retract ?f1)
    (assert (asset_result (+ ?r ?c)))
    )

(defrule combine_asset_choice_estate
    ?f1 <- (asset_score_estate ?c)
    ?f2 <- (asset_result ?r)
=>
    (retract ?f2)
    (retract ?f1)
    (assert (asset_result (+ ?r ?c)))
    )

(defrule asset_no
        (source_asset_yn n)
=>      (assert (asset_result 0))
)        

(defrule invest_input
    (logical (asset_result ?r))
=>
        (assert (UI-state (display invest_input)
                     (relation-asserted source_invest_yn)
                     (response y)    
                     (valid-answers y n))))

(defrule invest_yes
        (logical (source_invest_yn y))
=>      (assert (UI-state (display invest_yes)
                     (relation-asserted invest_choice)
                     (response invest1)    
                     (valid-answers invest1 invest2 invest3))))        

(defrule convert_invest_choice
    (logical (invest_choice ?c))
=>    (switch ?c 
        (case invest1 then (assert (invest_result 1)))
        (case invest2 then (assert (invest_result 2)))
        (case invest3 then (assert (invest_result 3))))
)

(defrule invest_no
        (logical (source_invest_yn n))
=>      (assert (invest_result 0))
)      
;;=============================================end of question part==========================================

;;=============================================reasoning part================================================
;;====================================START OF BACKGROUND=================================
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;Part II education, career, lifestyle
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;education question;;;;;;;;;;;;;;;;;;;

;;;;;After Hard Constrant, filter our non-necessary facts from education Initial facts
(defrule decrease_facts_edu
	(declare (salience 7))
	(target_gender ?g)
	(target_hard_fact (name ?name1) (gender ?gender) )
	(target_a_fact_bkgd (name ?name2&:(eq ?name1 ?name2)) (category education) (val ?edu) )
=>	
	(assert (target_a_current_bkgd (name ?name2) (category education) (val ?edu) ))
	(assert (current_goal (category education) (name ?name2) (cf 0.5)))
)


;;;;;delete all initial appearance facts(not required anymore)
(defrule delete_initial_facts_edu
	(declare (salience 6))
	(target_gender ?g)
	?f1 <-(target_a_fact_bkgd (name ?name) (category education) (val ?edu) )
=>
	(retract ?f1)
)

;;;;;;;;find target's with similar education level, increase CF. For other's decrease CF.
(defrule edu_filter
	(declare (salience 7))
	(source_edu ?s_edu)
	(target_gender ?tg)
	(target_a_current_bkgd (name ?name) (category education) (val ?t_edu) )
=>	
	(if (eq ?s_edu ?t_edu) then
	(assert (working_goal (category education) (name ?name) (cf  0.75)))
    (assert (reason (name ?name) (category background) (subcategory "Education")(reasondesc "You are at the same education level")))
    ) 
	else
	(if (and (eq ?tg m) (> ?s_edu ?t_edu)) then
	(assert (working_goal (category education) (name ?name) (cf (* (- ?s_edu ?t_edu) -0.2)))))
	else
	(if (and (eq ?tg m) (< ?s_edu ?t_edu)) then
	(assert (working_goal (category education) (name ?name) (cf (* (- ?t_edu ?s_edu) 0.1)))))
	else
	(if (and (eq ?tg f) (> ?s_edu ?t_edu)) then
	(assert (working_goal (category education) (name ?name) (cf (* (- ?s_edu ?t_edu) 0.1)))))
	else
	(if (and (eq ?tg f) (< ?s_edu ?t_edu)) then
	(assert (working_goal (category education) (name ?name) (cf (* (- ?t_edu ?s_edu) -0.2)))))	
)



;;;;;;;;;;;;;;;;end of education question;;;;;;;;;;;;


;;;;;;;;;;;;;;;;career question;;;;;;;;;;;;;;;;;;;
;;;;;After Hard Constrant, filter our non-necessary facts from career Initial facts
(defrule decrease_facts_car
	(declare (salience 7))
	(target_gender ?g)
	(target_hard_fact (name ?name1) (gender ?gender) )
	(target_a_fact_bkgd (name ?name2&:(eq ?name1 ?name2)) (category career) (val ?car) )
=>	
	(assert (target_a_current_bkgd (name ?name2) (category career) (val ?car) ))
	(assert (current_goal (category career) (name ?name2) (cf 0.5)))
)


;;;;;delete all initial appearance facts(not required anymore)
(defrule delete_initial_facts_car
	(declare (salience 6))
	(target_gender ?g)
	?f1 <-(target_a_fact_bkgd (name ?name) (category career) (val ?car) )
=>
	(retract ?f1)
)

;;;;;;;;find target's with same career, increase CF.
;;;;;;;;If either one is jobless (career number 14) decrease CF
;;;;;;;;If both are jobless, decrease large amount CF

(defrule car_filter
	(declare (salience 7))
	(source_car ?s_car)
	(target_a_current_bkgd (name ?name) (category career) (val ?t_car) )
=>	
	(if (and (eq ?s_car ?t_car) (neq ?s_car 14)) then
	(assert (working_goal (category career) (name ?name) (cf  0.75)))
    (assert (reason (name ?name) (category background) (subcategory "Career")(reasondesc "You work in the same field"))))
  (if (and (neq ?s_car ?t_car) (neq ?s_car 14)) then
	(assert (working_goal (category career) (name ?name) (cf  0.1))))
  (if (and (eq ?s_car ?t_car) (eq ?s_car 14)) then
	(assert (working_goal (category career) (name ?name) (cf  -0.8))))
  (if (and (neq ?s_car ?t_car) (or (eq ?s_car 14) (eq ?t_car 14))) then
	(assert (working_goal (category career) (name ?name) (cf  -0.3))))
)


;;;;;;;;;;;;;;;;;end of career question;;;;;;;;;;;;

;;;;;;;;;;;;;;;;Financial status question;;;;;;;;;;;;;;;;;;;











 

(defrule financial_result
        (declare (salience 7))
        (invest_result ?i_result)
        (asset_result ?a_result)
        (fin_result ?f_result)
=>      (assert (finan_result (+ ?i_result ?a_result ?f_result)))
)

;;;;;After Hard Constrant, filter our non-necessary facts from finance Initial facts
(defrule decrease_facts_fin
	(declare (salience 7))
	(target_gender ?g)
	(target_hard_fact (name ?name1) (gender ?gender) )
	(target_a_fact_bkgd (name ?name2&:(eq ?name1 ?name2)) (category finance) (val ?fin) )
=>	
	(assert (target_a_current_bkgd (name ?name2) (category finance) (val ?fin) ))
	(assert (current_goal (category finance) (name ?name2) (cf 0.5)))
)


;;;;;delete all initial appearance facts(not required anymore)
(defrule delete_initial_facts_finan
	(declare (salience 6))
	(target_gender ?g)
	?f1 <-(target_a_fact_bkgd (name ?name) (category finance) (val ?fin) )
=>
	(retract ?f1)
)

;;;;;;;;find target's with similar finanical status, increase CF. For other's depends on the difference between both.
(defrule fin_filter
	(declare (salience 7))
	(finan_result ?s_fin)
	(target_gender ?tg)
	(target_a_current_bkgd (name ?name) (category finance) (val ?t_fin) )
=>(if (eq ?s_fin ?t_fin) then
	(assert (working_goal (category finance) (name ?name) (cf 0.75)))
    (assert (reason (name ?name) (category background) (subcategory "Financial")(reasondesc "You are at the same financial level")))) 
	else
	(if (and (eq ?tg m) (> ?s_fin ?t_fin)) then
	(assert (working_goal (category finance) (name ?name) (cf (* (- ?s_fin ?t_fin) -0.05)))))
	else
	(if (and (eq ?tg m) (< ?s_fin ?t_fin)) then
	(assert (working_goal (category finance) (name ?name) (cf (* (- ?t_fin ?s_fin) 0.05)))))
	else
	(if (and (eq ?tg f) (> ?s_fin ?t_fin)) then
	(assert (working_goal (category finance) (name ?name) (cf (* (- ?s_fin ?t_fin) 0.05)))))
	else
	(if (and (eq ?tg f) (< ?s_fin ?t_fin)) then
	(assert (working_goal (category finance) (name ?name) (cf (* (- ?t_fin ?s_fin) -0.05)))))


)


;;;;;;;;;;;;;Transfer the education and career and financial status cf to the total 4 category cf facts.
(defrule backgroundto4
;	(declare (salience -9))
	(current_goal (category career) (name ?name_car) (cf ?cf_car))
	(current_goal (category education) (name ?name_edu) (cf ?cf_edu))
	(current_goal (category finance) (name ?name_fin) (cf ?cf_fin))
	?f1 <- (person_CFs (name ?name2&:(eq ?name_car ?name_edu ?name_fin ?name2)) (appearance-cf ?a-cf) (edujob-cf ?e-cf&:(neq ?e-cf (/ (+ ?cf_car ?cf_edu ?cf_fin) 3))) (character-cf ?c-cf) (horoscope-cf ?h-cf))
=>
	(assert (person_CFs (name ?name2) (appearance-cf ?a-cf) (edujob-cf (/ (+ ?cf_car ?cf_edu ?cf_fin) 3)) (character-cf ?c-cf) (horoscope-cf ?h-cf)))
	(retract ?f1)
    		    (assert (begin_psnlt));;start personality
)



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;End of Part II education, career, lifestyle
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;====================================END OF BACKGROUND=================================
